var table;
function init() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd hh:ii',
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd hh:ii',
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	initData();
}
function initData() {
	var start = $('#start').val();
	var end = $('#end').val();
	var loading = layer.load(1, {
	  shade: [0.5,'#fff'] //0.1透明度的白色背景
	});
	$.getJSON("/rest/thunder/getThunderWarningTableData/" + start
			+ "/" + end +"/"+$('#voltage').val(), function(result) {
		layer.close(loading);
		loadTable(result.tableData);
	});
}
function loadTable(result) {
	if (table) {
		table.getTable().destroy();
	}
	var columns = ['序号','设备','次数','详情 '];
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.searching = true;
	opt.scrollY = 500;
	table = new MyTable("datatable", columns, opt);
	var data = [];
	for(var i in result){
		var opt = '<button onclick="gotoDetail(this)" data-id="'+result[i][2]+'" data-name="'+result[i][0]+'" class="btn btn-primary fa fa-search"/>';
		data.push([Number(i)+1,result[i][0],result[i][1],opt]);
	}
	table.setDataSet(data).show();
}
function query(){
	initData();	
}
function gotoDetail(ele) {
	var fra = document.createElement("iframe");
	fra.style = "background:transparent;border:0;";
	fra.width = "100%";
	fra.id = "iframe";
	fra.height = "650px";
	var eid = $(ele).attr('data-id');
	var ename = $(ele).attr('data-name');
	ename = encodeURI(encodeURI(ename));
	var s = "/ui/map/dialog/thunderwarning/thunderFaultDialog.xhtml?";
	s += "eid="+eid+"&ename="+ename+"&showBackBtn=true&startTime="+$('#start').val()+"&endTime="+$('#end').val();
	fra.src = s;
	document.body.appendChild(fra);
	$('#tableDiv').css('display', 'none');
};
function backFromChild() {
	document.body.removeChild(document.getElementById("iframe"));
	$('#tableDiv').css('display', 'block');
	table.getTable().draw();
}